package com.tjdr.his.drugstore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tjdr.his.common.result.HttpResp;
import com.tjdr.his.common.result.ResultCode;
import com.tjdr.his.domain.entity.StorageInfo;
import com.tjdr.his.drugstore.service.IStorageInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR 张三
 * @DATE 2024/10/18 10:18
 * @DESCRIBER
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/storage")
public class StorageInfoController {
    @Autowired
    private IStorageInfoService service;

    /**
     * 查询库存
     * @param value 库存的编码或者药品名称
     * @param drugType 药品类型
     * @param pageSize 每页的数量
     * @param currentPage 页码
     * @return
     */
    @GetMapping("getStoargeInfoByCondition/{value}/{drugType}/{pageSize}/{currentPage}")
    public HttpResp getStoargeInfoByCondition(@PathVariable String value, @PathVariable String drugType,@PathVariable int pageSize,@PathVariable int currentPage){
        log.debug("value:{},durgType:{},pageSize:{},currentPage:{}",value,drugType,pageSize,currentPage);
        IPage page =service.selectStorageInfoByProperty(value,drugType,pageSize,currentPage);
        Map map = new HashMap<>();
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,map);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }

    @PostMapping("addStoargeInfo")
    public HttpResp addStoargeInfo(StorageInfo storageInfo){
        log.debug("storageInfo:{}",storageInfo);
        int count  =service.insertStorageInfo(storageInfo);
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,count);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }
    @PutMapping("updateStoargeInfoById")
    public HttpResp updateStoargeInfo(@RequestBody StorageInfo storageInfo){
        log.debug("storageInfo:{}",storageInfo);
        int count  =service.updateStorageById(storageInfo);
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,count);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }
    @DeleteMapping("deleteStoargeInfoById")
    public HttpResp deleteStoargeInfoById(StorageInfo storageInfo){
        log.debug("storageInfo:{}",storageInfo);
        int count  =service.deleteStorageById(storageInfo);
        HttpResp httpResp = HttpResp.createHttpRespInstance(ResultCode.OPTION_SUCCESS,count);
        log.debug("httpResp:{}",httpResp);
        return httpResp;
    }
}
